home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / THIN C 2.0 / Projects / windowMaker / windowMaker.c next >
Encoding:
C/C++ Source or Header  |  1994-02-01  |  9.4 KB  |  438 lines  |  [TEXT/THIN]

  1. /********************************************************/
  2. /*                                                        */
  3. /*  WindowMaker Code from Chapter Seven of                 */
  4. /*                                                        */
  5. /*          *** The Macintosh Programming Primer ***        */
  6. /*                                                      */
  7. /*    Copyright 1989, Dave Mark and Cartwright Reed       */
  8. /*                                                        */
  9. /*  WindowMaker handles desk accessories, as well        */
  10. /*    as error checking and a few other things.  Since    */
  11. /*    we went to press, we've made a few minor changes    */
  12. /*    to WindowMaker.  The changes are commented and are    */
  13. /*    found in the HandleEvent routine.                    */    
  14. /*                                                        */    
  15. /*    The WindowMaker project is a good place to start    */
  16. /*    with your own standalone application code.          */
  17. /*                                                        */
  18. /********************************************************/   
  19.  
  20. #define BASE_RES_ID            400
  21. #define    NIL_POINTER            0L
  22. #define    MOVE_TO_FRONT        (WindowPtr)-1L
  23. #define    REMOVE_ALL_EVENTS    0
  24.  
  25. #define APPLE_MENU_ID        400
  26. #define FILE_MENU_ID        401
  27. #define EDIT_MENU_ID        402
  28.  
  29. #define ABOUT_ITEM            1
  30. #define    ABOUT_ALERT            400
  31. #define    ERROR_ALERT_ID        401
  32.  
  33. #define    NO_MBAR                BASE_RES_ID
  34. #define    NO_MENU                BASE_RES_ID+1
  35. #define    NO_PICTURE            BASE_RES_ID+2
  36. #define    NO_WIND                BASE_RES_ID+3
  37.  
  38. #define    NEW_ITEM            1
  39. #define    CLOSE_ITEM            2
  40. #define    QUIT_ITEM            3
  41.  
  42. #define UNDO_ITEM            1
  43. #define CUT_ITEM            3
  44. #define COPY_ITEM            4
  45. #define PASTE_ITEM            5
  46. #define CLEAR_ITEM            6
  47.  
  48. #define    DRAG_THRESHOLD        30
  49.  
  50. #define    WINDOW_HOME_LEFT    5
  51. #define    WINDOW_HOME_TOP        45
  52. #define    NEW_WINDOW_OFFSET    20
  53.  
  54. #define MIN_SLEEP            0L
  55. #define    NIL_MOUSE_REGION    0L
  56.  
  57. #define    LEAVE_WHERE_IT_IS    FALSE
  58.  
  59. #define    WNE_TRAP_NUM        0x60
  60. #define    UNIMPL_TRAP_NUM        0x9F
  61.  
  62. #define    NIL_STRING                "\p"
  63. #define    HOPELESSLY_FATAL_ERROR    "\pGame over, man!"
  64.  
  65. /* KI */
  66. #include <THINK.h>
  67. #include <Quickdraw.h>
  68. #include <Windows.h>
  69. #include <Events.h>
  70. #include <OSEvents.h>
  71. #include <Menus.h>
  72. #include <ToolUtils.h>
  73. #include <Dialogs.h>
  74. #include <OSUtils.h>
  75. #include <Desk.h>
  76. #include <Memory.h>
  77. #include <Fonts.h>
  78.  
  79.  
  80. Boolean        gDone, gWNEImplemented;
  81. EventRecord    gTheEvent;
  82. MenuHandle    gAppleMenu, gEditMenu;
  83. PicHandle    gMyPicture;
  84. Rect        gDragRect;
  85. int            gNewWindowLeft = WINDOW_HOME_LEFT, gNewWindowTop = WINDOW_HOME_TOP;
  86.  
  87.  
  88. /******************************** main *********/
  89.  
  90. main()
  91. {
  92.     ToolBoxInit();
  93.     MenuBarInit();
  94.     LoadPicture();
  95.     SetUpDragRect();
  96.  
  97.     MainLoop();
  98. }
  99.  
  100. /*********************************** ToolBoxInit */
  101.  
  102. ToolBoxInit()
  103. {
  104.     InitGraf( &thePort );
  105.     InitFonts();
  106.     FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
  107.     InitWindows();
  108.     InitMenus();
  109.     TEInit();
  110.     InitDialogs( NIL_POINTER );
  111.     InitCursor();
  112. }
  113.  
  114.  
  115. /***********************************    MenuBarInit    */
  116.  
  117. MenuBarInit()
  118. {
  119.     Handle        myMenuBar;
  120.  
  121.     if ( ( myMenuBar = GetNewMBar( BASE_RES_ID ) ) == NIL_POINTER )
  122.         ErrorHandler( NO_MBAR );
  123.     SetMenuBar( myMenuBar );
  124.     if ( ( gAppleMenu = GetMHandle( APPLE_MENU_ID ) ) == NIL_POINTER )
  125.         ErrorHandler( NO_MENU );
  126.     if ( ( gEditMenu = GetMHandle( EDIT_MENU_ID ) ) == NIL_POINTER )
  127.         ErrorHandler( NO_MENU );
  128.  
  129.     AddResMenu( gAppleMenu, 'DRVR' );
  130.     DrawMenuBar();
  131. }
  132.  
  133.  
  134. /******************************** LoadPicture *********/
  135.  
  136. LoadPicture()
  137. {
  138.  
  139.     if ( ( gMyPicture = GetPicture( BASE_RES_ID ) ) == NIL_POINTER )
  140.         ErrorHandler( NO_PICTURE );
  141. }
  142.  
  143.  
  144. /******************************** SetUpDragRect *********/
  145.  
  146. SetUpDragRect()
  147. {
  148.     gDragRect = screenBits.bounds;
  149.     gDragRect.left += DRAG_THRESHOLD;
  150.     gDragRect.right -= DRAG_THRESHOLD;
  151.     gDragRect.bottom -= DRAG_THRESHOLD;
  152. }
  153.  
  154.  
  155. /******************************** MainLoop *********/
  156.  
  157. MainLoop()
  158. {
  159.     gDone = FALSE;
  160.     gWNEImplemented = ( GetToolTrapAddress( WNE_TRAP_NUM ) !=
  161.                         GetToolTrapAddress( UNIMPL_TRAP_NUM ) );
  162.     while ( gDone == FALSE )
  163.     {
  164.         HandleEvent();
  165.     }
  166. }
  167.  
  168.  
  169. /************************************* HandleEvent     */
  170.  
  171. HandleEvent()
  172. {
  173.     char    theChar;
  174.     GrafPtr    oldPort;        /*    This variable is used in updateEvt handling -
  175.                                 It is not in the book...    */
  176.             
  177.     if ( gWNEImplemented )
  178.         WaitNextEvent( everyEvent, &gTheEvent, MIN_SLEEP, NIL_MOUSE_REGION );
  179.     else
  180.     {
  181.         SystemTask();
  182.         GetNextEvent( everyEvent, &gTheEvent );
  183.     }
  184.  
  185.     switch ( gTheEvent.what )
  186.     {
  187.         case mouseDown: 
  188.             HandleMouseDown();
  189.             break;
  190.         case keyDown:
  191.         case autoKey:
  192.             theChar = gTheEvent.message & charCodeMask;
  193.             if (( gTheEvent.modifiers & cmdKey ) != 0)
  194.             {
  195.                 AdjustMenus(); 
  196.                 HandleMenuChoice( MenuKey( theChar ) );
  197.             }
  198.             break;
  199.         case updateEvt:
  200.             /*    This code is different than that found in the book -
  201.                 Use this version...    */
  202.             if (!IsDAWindow( gTheEvent.message ) )
  203.             {
  204.                 GetPort( &oldPort );
  205.                 SetPort( (GrafPtr)gTheEvent.message );
  206.                 BeginUpdate( (WindowPtr)gTheEvent.message );
  207.                 DrawMyPicture( gMyPicture, gTheEvent.message );
  208.                 EndUpdate( (WindowPtr)gTheEvent.message );
  209.                 SetPort( oldPort );
  210.             }
  211.             break;
  212.     }
  213. }
  214.  
  215.  
  216. /************************************* HandleMouseDown */
  217.  
  218. HandleMouseDown()
  219. {
  220.     WindowPtr    whichWindow;
  221.     short int    thePart;
  222.     long int    menuChoice, windSize;
  223.     
  224.     thePart = FindWindow( gTheEvent.where, &whichWindow );
  225.     switch ( thePart )
  226.     {
  227.         case inMenuBar:
  228.             AdjustMenus();
  229.             menuChoice = MenuSelect( gTheEvent.where );
  230.             HandleMenuChoice( menuChoice );
  231.             break;
  232.         case inSysWindow: 
  233.             SystemClick( &gTheEvent, whichWindow );
  234.             break;
  235.         case inDrag: 
  236.             DragWindow( whichWindow, gTheEvent.where, &gDragRect );
  237.             break;
  238.         case inGoAway: 
  239.             if ( TrackGoAway( whichWindow, gTheEvent.where ) )
  240.                 DisposeWindow( whichWindow );
  241.             break;
  242.         case inContent:
  243.             SelectWindow( whichWindow );
  244.             break;
  245.     }
  246. }
  247.  
  248. /************************************* AdjustMenus */
  249.  
  250. AdjustMenus()
  251. {
  252.     if (IsDAWindow( FrontWindow() ) )
  253.     {
  254.         EnableItem(gEditMenu, UNDO_ITEM );
  255.         EnableItem(gEditMenu, CUT_ITEM );
  256.         EnableItem(gEditMenu, COPY_ITEM );
  257.         EnableItem(gEditMenu, PASTE_ITEM );
  258.         EnableItem(gEditMenu, CLEAR_ITEM );
  259.     }
  260.     else
  261.     {
  262.         DisableItem(gEditMenu, UNDO_ITEM );
  263.         DisableItem(gEditMenu, CUT_ITEM );
  264.         DisableItem(gEditMenu, COPY_ITEM );
  265.         DisableItem(gEditMenu, PASTE_ITEM );
  266.         DisableItem(gEditMenu, CLEAR_ITEM );
  267.     }
  268. }
  269.  
  270.  
  271. /************************************* IsDAWindow */
  272.  
  273. IsDAWindow( whichWindow )
  274. WindowPtr    whichWindow;
  275. {
  276.     if ( whichWindow == NIL_POINTER )
  277.         return( FALSE );
  278.     else /* DA windows have negative windowKinds */
  279.         return( ( (WindowPeek)whichWindow )->windowKind < 0 );
  280. }
  281.  
  282.  
  283. /************************************* HandleMenuChoice */
  284.  
  285. HandleMenuChoice( menuChoice )
  286. long int    menuChoice;
  287. {
  288.     int    theMenu;
  289.     int    theItem;
  290.     
  291.     if ( menuChoice != 0 )
  292.     {
  293.         theMenu = HiWord( menuChoice );
  294.         theItem = LoWord( menuChoice );
  295.         switch ( theMenu )
  296.         {
  297.             case APPLE_MENU_ID :
  298.                 HandleAppleChoice( theItem );
  299.                 break;
  300.             case FILE_MENU_ID :
  301.                 HandleFileChoice( theItem );
  302.                 break;
  303.             case EDIT_MENU_ID :
  304.                 HandleEditChoice( theItem );
  305.                 break;
  306.         }
  307.         HiliteMenu( 0 );
  308.     }
  309. }
  310.  
  311.  
  312. /********************************    HandleAppleChoice    *******/
  313.  
  314. HandleAppleChoice( theItem )
  315. int    theItem;
  316. {
  317.     Str255        accName;
  318.     int            accNumber;
  319.     
  320.     switch ( theItem )
  321.     {
  322.         case ABOUT_ITEM : 
  323.             NoteAlert( ABOUT_ALERT, NIL_POINTER );
  324.             break;
  325.         default :
  326.             GetItem( gAppleMenu, theItem, accName );
  327.             accNumber = OpenDeskAcc( accName );
  328.             break;
  329.     }
  330. }
  331.  
  332.  
  333. /********************************    HandleFileChoice    *******/
  334.  
  335. HandleFileChoice( theItem )
  336. int    theItem;
  337. {
  338.     WindowPtr    whichWindow;
  339.     switch ( theItem )
  340.     {
  341.         case NEW_ITEM :
  342.             CreateWindow();
  343.             break;
  344.         case CLOSE_ITEM :
  345.             if ( ( whichWindow = FrontWindow() ) != NIL_POINTER )
  346.                 DisposeWindow( whichWindow );
  347.             break;
  348.         case QUIT_ITEM :
  349.             gDone = TRUE;
  350.             break;
  351.     }
  352. }
  353.  
  354.  
  355. /********************************    HandleEditChoice    *******/
  356.  
  357. HandleEditChoice( theItem )
  358. int    theItem;
  359. {
  360.     SystemEdit( theItem - 1 );
  361. }
  362.  
  363.  
  364. /************************************ CreateWindow  */
  365.  
  366. CreateWindow()
  367. {
  368.     WindowPtr    theNewestWindow;
  369.     
  370.     if ( ( theNewestWindow = GetNewWindow( BASE_RES_ID, NIL_POINTER,
  371.                                 MOVE_TO_FRONT ) ) == NIL_POINTER )
  372.         ErrorHandler( NO_WIND );
  373.     if ( ( (screenBits.bounds.right - gNewWindowLeft) < DRAG_THRESHOLD ) ||
  374.          ( ( screenBits.bounds.bottom - gNewWindowTop) < DRAG_THRESHOLD ) )
  375.     {
  376.         gNewWindowLeft = WINDOW_HOME_LEFT;
  377.         gNewWindowTop = WINDOW_HOME_TOP;
  378.     }
  379.     
  380.     MoveWindow( theNewestWindow, gNewWindowLeft, gNewWindowTop, LEAVE_WHERE_IT_IS );
  381.     gNewWindowLeft += NEW_WINDOW_OFFSET;
  382.     gNewWindowTop += NEW_WINDOW_OFFSET;
  383.     ShowWindow( theNewestWindow );
  384. }
  385.  
  386.  
  387. /******************************** DrawMyPicture *********/
  388.  
  389. DrawMyPicture( thePicture, pictureWindow )
  390. PicHandle    thePicture;
  391. WindowPtr    pictureWindow;
  392. {
  393.     Rect    myRect;
  394.     
  395.     myRect = pictureWindow->portRect;
  396.     CenterPict( thePicture, &myRect );
  397.     SetPort( pictureWindow );
  398.     DrawPicture( thePicture, &myRect );
  399. }
  400.  
  401.  
  402. /******************************** CenterPict *********/
  403.  
  404. CenterPict( thePicture, myRectPtr )
  405. PicHandle    thePicture;
  406. Rect        *myRectPtr;
  407. {
  408.     Rect    windRect, pictureRect;
  409.     
  410.     windRect = *myRectPtr;
  411.     pictureRect = (**( thePicture )).picFrame;
  412.     myRectPtr->top = (windRect.bottom - windRect.top - (pictureRect.bottom - pictureRect.top))
  413.         / 2 + windRect.top;
  414.     myRectPtr->bottom = myRectPtr->top + (pictureRect.bottom - pictureRect.top);
  415.     myRectPtr->left = (windRect.right - windRect.left - (pictureRect.right - pictureRect.left))
  416.         / 2 + windRect.left;
  417.     myRectPtr->right = myRectPtr->left + (pictureRect.right - pictureRect.left);
  418. }
  419.  
  420.  
  421. /******************************** ErrorHandler *********/
  422.  
  423. ErrorHandler( stringNum )
  424. int    stringNum;
  425. {
  426.     StringHandle    errorStringH;
  427.     
  428.     if ( ( errorStringH = GetString( stringNum ) ) == NIL_POINTER )
  429.         ParamText( HOPELESSLY_FATAL_ERROR, NIL_STRING, NIL_STRING, NIL_STRING );
  430.     else
  431.     {
  432.         HLock( errorStringH );
  433.         ParamText( *errorStringH, NIL_STRING, NIL_STRING, NIL_STRING );
  434.         HUnlock( errorStringH );
  435.     }
  436.     StopAlert( ERROR_ALERT_ID, NIL_POINTER );
  437.     ExitToShell();
  438. }